package com.handpay.framework.upcard.sdom;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

import com.handpay.zztong.hp.log.HPLog;

/**
 * @author <a href="mailto:justin.hou@handpay.com.cn">Sora</a>
 * @version 1.0
 * 
 *          XML的DOM解析实现
 */
public class DomLiteParser
{
	public DomLiteParser()
	{
		xml_parser = null;
	}
	
	/**
	 * 根据数据流Reader解析XML
	 * 
	 * @param reader
	 *            XML数据流的Reader
	 * @return 解析后XML的根节点
	 * @throws XmlPullParserException
	 *             解析出错时抛出
	 * @throws IOException
	 *             出现IO异常时抛出
	 */
	public TagNode parse(InputStreamReader reader)
			throws XmlPullParserException, IOException
	{
		TagNode root = null;
		
		xml_parser = new KXmlParser();
		
		xml_parser.setInput(reader);
		
		root = build();
		
		return root;
	}
	
	private TagNode build() throws XmlPullParserException, IOException
	{
		TagNode root = null;
		TagNode child = null;
		TagNode sibling = null;
		
		boolean done = false;
		
		do
		{
			int event = xml_parser.next();
			
			HPLog.d("DomLiteParser", "event=" + event);
			
			switch (event)
			{
			// case KXmlParser.START_DOCUMENT:
			// break;
				case KXmlParser.START_TAG:
				{
					root = buildTagNode();
					
					for (TagNode t = build(); !(t.getTagType() == TagNode.TAG_END); t = build())
					{
						if (child == null)
						{
							child = t;
							sibling = child;
							
						}
						else
						{
							sibling.setSibling(t);
							sibling = t;
							
							HPLog.d("DomLiteParser", "<" + child.getName()
									+ ">'s sibling is " + t.getName());
						}
					}
					
					root.setChild(child);
					if (child != null)
						HPLog.d("DomLiteParser", "<" + root.getName()
								+ ">'s child is " + child.getName());
					else
						HPLog.d("DomLiteParser", "<" + root.getName()
								+ ">'s child is null");
					done = true;
				}
					break;
				case KXmlParser.TEXT:
				{
					String text = xml_parser.getText().trim();
					HPLog.d("DomLiteParser", "text=" + text);
					if (text.length() > 0)
					{
						root = new TagNode(text, TagNode.TAG_TEXT);
						done = true;
					}
				}
					break;
				case KXmlParser.END_TAG:
				{
					String name = xml_parser.getName();
					HPLog.d("DomLiteParser", "end tag = " + name);
					root = new TagNode(name, TagNode.TAG_END);
					done = true;
				}
					break;
				case KXmlParser.END_DOCUMENT:
				{
					root = new TagNode("END DOCUMENT", TagNode.TAG_END);
					done = true;
				}
					break;
			}
		}
		while (!done);
		
		return root;
	}
	
	private TagNode buildTagNode()
	{
		String tagName = xml_parser.getName();
		TagNode tag = new TagNode(tagName, TagNode.TAG_START);
		
		HPLog.d("DomLiteParser", "tag name=" + tagName);
		
		int attrCount = xml_parser.getAttributeCount();
		Hashtable<String, String> attrs = new Hashtable<String, String>();
		for (int i = 0; i < attrCount; i++)
		{
			String attrName = xml_parser.getAttributeName(i);
			String attrValue = xml_parser.getAttributeValue(i);
			HPLog.d("DomLiteParser", attrName + "=" + attrValue);
			attrs.put(attrName, attrValue);
		}
		
		tag.setAttributes(attrs);
		
		return tag;
	}
	
	private KXmlParser	xml_parser;
}
